package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.QuickPaytwoDeleagte;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.ExampleUtil;
import com.tchintech.pay.utils.PrintUtil;
import com.tchintech.pay.utils.TimeUtil;
import com.tchintech.pay.view.fragment.MainFragment;
import com.tchintech.pay.widget.DialogWiget;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/12 0012.
 * <p>
 * 测试页面
 * 梁崇彪
 */

public class QuickPaytwoActivity extends ActivityPresenter<QuickPaytwoDeleagte> implements View.OnClickListener {
    private Intent intent;
    ;
    private String cardUrl = "FastPayment.quick";
    private Map<String, String> map;
    private Handler handler;
    private String card;
    private PrintUtil printUtil;


    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this, R.id.bt_1, R.id.bt_2, R.id.bt_3, R.id.bt_4, R.id.bt_5);

    }


    @Override
    protected void initData() {
        super.initData();
        intent = new Intent();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_1:
                // printUtil = new PrintUtil(this);
                break;
            case R.id.bt_2:
                //initShareIntent();
                String pkName = this.getPackageName();
                String model = android.os.Build.MODEL;
                String xh = Settings.System.getString(context.getContentResolver(), "device_code");
                showMsg(xh);
                break;
            case R.id.bt_3:
                if (TimeUtil.isDeal()) {
                    DialogWiget dialog = new DialogWiget();
                    dialog.showMessage(this, "无法交易", "交易时段为每天08:00~22:00");
                    return;
                } else {
                    showMsg("不再交易时段内");
                }
                showMsg("l啦啦啦");

                break;
            case R.id.bt_4:
                JPushInterface.resumePush(getApplicationContext());
                break;
            case R.id.bt_5:
                String rid = JPushInterface.getRegistrationID(getApplicationContext());
                if (!rid.isEmpty()) {

                    showMsg("RegId:" + rid);
                } else {
                    Toast.makeText(this, "Get registration fail, JPush init failed!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    // 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
    private void init() {
        String appKey = ExampleUtil.getAppKey(getApplicationContext());
        if (null == appKey) appKey = "AppKey异常";
        showMsg(appKey);
        JPushInterface.init(getApplicationContext());
    }

    /**
     * 调用系统分享
     */
    private void initShareIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
// 查询所有可以分享的Activity
        List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (!resInfo.isEmpty()) {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            for (ResolveInfo info : resInfo) {
                Intent targeted = new Intent(Intent.ACTION_SEND);
                targeted.setType("text/plain");
                ActivityInfo activityInfo = info.activityInfo;
                String phone = AppConst.getUser().getPhone();
                // 分享出去的内容
                targeted.putExtra(Intent.EXTRA_TEXT, "这是我的分享内容" + getPackageName());
                // 分享出去的标题
                targeted.putExtra(Intent.EXTRA_SUBJECT, "主题");
                targeted.setPackage(activityInfo.packageName);
                targeted.setClassName(activityInfo.packageName, info.activityInfo.name);
                PackageManager pm = this.getApplication().getPackageManager();
                // 微信有2个怎么区分-。- 朋友圈还有微信
                if (info.activityInfo.applicationInfo.loadLabel(pm).toString().equals("微信")) {
                    targetedShareIntents.add(targeted);
                }
            }
            // 选择分享时的标题
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "选择分享");
            if (chooserIntent == null) {
                return;
            }
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
            try {
                startActivity(chooserIntent);
            } catch (android.content.ActivityNotFoundException ex) {

                Toast.makeText(this, "找不到该分享应用组件", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected Class<QuickPaytwoDeleagte> getDelegateClass() {
        return QuickPaytwoDeleagte.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
